package com.mall.coupon.service.impl;

import com.mall.common.dto.MemberPriceDTO;
import com.mall.common.dto.SkuReductionDTO;
import com.mall.coupon.entity.MemberPriceEntity;
import com.mall.coupon.entity.SkuFullReductionEntity;
import com.mall.coupon.entity.SkuLadderEntity;
import com.mall.coupon.service.FeignService;
import com.mall.coupon.service.MemberPriceService;
import com.mall.coupon.service.SkuFullReductionService;
import com.mall.coupon.service.SkuLadderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 谢君臣
 * @Date: 2022/7/25-17:06
 * @version: 1.0
 * @function:
 */
@Service
public class FeignServiceImpl implements FeignService {

    @Resource
    private SkuLadderService skuLadderService;
    @Resource
    private SkuFullReductionService skuFullReductionService;
    @Resource
    private MemberPriceService memberPriceService;


    @Override
    public void saveSkuReduction(SkuReductionDTO skuReductionDTO) {
        Long skuId = skuReductionDTO.getSkuId();
        // 折扣
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuId);
        skuLadderEntity.setFullCount(skuReductionDTO.getFullCount());
        skuLadderEntity.setDiscount(skuReductionDTO.getDiscount());
        skuLadderEntity.setAddOther(skuReductionDTO.getCountStatus());
        if (skuLadderEntity.getFullCount().compareTo(new BigDecimal("0")) == 1)
            skuLadderService.insert(skuLadderEntity);
        // 满减
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        //  BeanUtil.copyProperties(skuReductionDTO,skuFullReductionEntity);
        skuFullReductionEntity.setSkuId(skuReductionDTO.getSkuId());
        skuFullReductionEntity.setFullPrice(skuReductionDTO.getFullPrice());
        skuFullReductionEntity.setFullPrice(skuReductionDTO.getFullPrice());
        skuFullReductionEntity.setAddOther(skuReductionDTO.getCountStatus());
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1)
            skuFullReductionService.insert(skuFullReductionEntity);

        // 会员价格
        List<MemberPriceDTO> memberPriceDTOS = skuReductionDTO.getMemberPrice();
        List<MemberPriceEntity> priceEntities = memberPriceDTOS.stream().map(memberPriceDTO -> {
            MemberPriceEntity memberPrice = new MemberPriceEntity();
            memberPrice.setSkuId(skuId);
            memberPrice.setAddOther(memberPriceDTO.getAddOther());
            memberPrice.setMemberPrice(memberPriceDTO.getMemberPrice());
            memberPrice.setMemberLevelId(memberPriceDTO.getMemberLevelId());
            memberPrice.setMemberLevelName(memberPriceDTO.getMemberLevelName());
            return memberPrice;
        }).filter(item -> item.getMemberPrice().compareTo(new BigDecimal("0")) == 1).collect(Collectors.toList());
        memberPriceService.insertBatch(priceEntities);
    }
}
